package com.quectel.aiot.config;

import com.quectel.aiot.service.ProductService;
import com.quectel.aiot.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Configuration
@EnableScheduling
@Slf4j
public class SyncScheduler {
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    private DeviceService deviceService;
    
    @Value("${sync.products.enabled:true}")
    private boolean productsSyncEnabled;
    @Value("${sync.products.cron:0 */3 * * * ?}")
    private String productsSyncCron;
    @Value("${sync.devices.enabled:true}")
    private boolean devicesSyncEnabled;
    @Value("${sync.devices.cron:0 */5 * * * ?}")
    private String devicesSyncCron;
    
    private final Lock productSyncLock = new ReentrantLock();
    private final Lock deviceSyncLock = new ReentrantLock();
    
    public String getProductsSyncCron() {
        return productsSyncCron;
    }
    public String getDevicesSyncCron() {
        return devicesSyncCron;
    }
    
    // 每30分钟同步一次产品列表
    @Scheduled(cron = "#{@syncScheduler.productsSyncCron}")
    public void syncProducts() {
        if (!productsSyncEnabled) {
            log.info("产品同步定时任务已关闭");
            return;
        }
        if (!productSyncLock.tryLock()) {
            log.warn("上一个产品同步任务还在执行中，本次同步跳过");
            return;
        }
        
        try {
            log.info("开始同步产品列表...");
            long startTime = System.currentTimeMillis();
            
            productService.syncAllProjectsProducts();
            
            long endTime = System.currentTimeMillis();
            log.info("产品列表同步完成，耗时：{}ms", (endTime - startTime));
            
        } catch (Exception e) {
            log.error("产品列表同步失败", e);
        } finally {
            productSyncLock.unlock();
        }
    }
    
    // 每5分钟同步一次设备列表
    @Scheduled(cron = "#{@syncScheduler.devicesSyncCron}")
    public void syncDevices() {
        if (!devicesSyncEnabled) {
            log.info("设备同步定时任务已关闭");
            return;
        }
        if (!deviceSyncLock.tryLock()) {
            log.warn("上一个设备同步任务还在执行中，本次同步跳过");
            return;
        }
        
        try {
            log.info("开始同步设备列表...");
            long startTime = System.currentTimeMillis();
            
            deviceService.syncAllProductsDevices();
            
            long endTime = System.currentTimeMillis();
            log.info("设备列表同步完成，耗时：{}ms", (endTime - startTime));
            
        } catch (Exception e) {
            log.error("设备列表同步失败", e);
        } finally {
            deviceSyncLock.unlock();
        }
    }
} 